package neuron.library.ga.selection;

import java.util.Random;

import neuron.library.ga.AbstractGeneticMachine;
import neuron.library.ga.common.Population;

/*
 * This strategy takes two random chromosomes and kills worst of them.
 */
public class TournamentSelection extends SelectionStrategy {

	public TournamentSelection(AbstractGeneticMachine gm) {
		super(gm);
	}

	@Override
	public void selection(Population population) {
		Random rand = new Random();
		int firstChromosome;
		int secondChromosome;
		population.calculateFitness();
		
		while (population.size() > gm.getConfig().getPopulatonSize()) {
			firstChromosome = rand.nextInt(population.size());
			secondChromosome = rand.nextInt(population.size());
			
			if (population.get(firstChromosome).getScore() >= population.get(secondChromosome).getScore())
				population.remove(secondChromosome);
			else
				population.remove(firstChromosome);	
		}
	}

}
